// pages/com/coursecom.js
const app=getApp()
Component({
        // observers: {
        //         'courseid': function(numberA) {
        //           // 在 numberA 或者 numberB 被设置时，执行这个函数
        //           this.setData({
        //             courseid:numberA
        //           })
        //         }
        //       },
lifetimes:{

      
       async attached(){
               console.log( 'label->',wx.getStorageSync('UserInfo'));
let that=this
                this.setData({
                        url:app.globalData.URL,
                        userid:wx.getStorageSync('UserInfo').id,
                        courseid:this.properties.courseid
                    
                      
                })
                var interval =   setInterval( async function(){
                                
                        if(that.properties.courseid!=0){    

                                that.setData({
                                   
                                        courseid:that.properties.courseid
                                    
                                      
                                })

                                await  that.getcoursetalk()
                            clearInterval(interval);    
                        }
                        
                    }, 1000);

                // 在组件实例进入页面节点树时执行
            


        }
},
        /**
         * 组件的属性列表
         */
        properties: {
            
                courseid: Number
        },

        /**
         * 组件的初始数据
         */
        data: {
                courseTalkList: [],
                url:'',
                show: false,
                message:'',
                parentid:0,
                talkid:0,
                userid:0,
                courseid:0,
                replyVisibility: [], // 控制每个问题的回复展开状态

               
        },

        /**
         * 组件的方法列表
         */
        methods: {
                toggleReply(e) {
                        const index = e.currentTarget.dataset.index;
                        const visibility = `replyVisibility[${index}]`;
                        this.setData({
                          [visibility]: !this.data.replyVisibility[index]
                        });
                      },
                onChange(e) {
                       
                    this.setData({
                            message:e.detail
                    })
                       
                      },
                reply(e){
                        console.log( 'e->',e.currentTarget.dataset);
                       
this.setData({
        show:true,
        parentid:e.currentTarget.dataset.parentid,
        talkid:e.currentTarget.dataset.talkid
    
})

                },
              async  getUserInfo() {
                        let now = new Date();
let year = now.getFullYear();
let month = (now.getMonth() + 1).toString().padStart(2, '0');
let day = now.getDate().toString().padStart(2, '0');
let hours = now.getHours().toString().padStart(2, '0');
let minutes = now.getMinutes().toString().padStart(2, '0');
let seconds = now.getSeconds().toString().padStart(2, '0');

let formattedTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
                        let t=this
                       console.log( 't.data.message->',t.data.message);
                       console.log( 't.data.userid->',t.data.userid);

                        let aa=()=>{
                                return new Promise((reslove,reject)=>{
                                        wx.request({
                                                url: t.data.url+'/courseReply/insert',
                                                method:"POST",
                                                data:{
                                                   replyText:t.data.message,
                                                   createTime:formattedTime,
                                                   talkId:t.data.talkid,
                                                   userId:t.data.userid,
                                                   parentId:t.data.parentid
                                                },
                                                success(res){
                                                        if (res.data.code==1) {
                                                                wx.showToast({
                                                                  title: '回复成功',
                                                                })
                                                                
                                                        }else{
                                                           wx.showToast({
                                                                   title: '回复失败',
                                                                 })
                                                                 reject(1)
                                                        }
                                                },complete(){
                                                    reslove(1)    
                                                }
                                              })
                                })
                        }
               await aa()
await t.getcoursetalk()

                      },
                      onClose() {
                        this.setData({ show: false,
                        message:'' });
                      },

                //回复
                getcoursetalk() {
                        let that=this
                        return new Promise((reslove, reject) => {
                                console.log( 'that.data.courseid->',that.data.courseid);
                                wx.request({
                                        url: that.data.url + `/courseTalk/selectAll`,
                                        method:"GET",
                                        data:{
                                                courseId:that.data.courseid
                                        },
                                        success(resp) {
                                                console.log("得到问题列表", resp.data.data.list);
                                                that.setData({
                                                        courseTalkList: resp.data.data.list
                                                })

                                        },
                                        complete() {
                                                reslove(1)
                                        }
                                })
                        })
                }

        }
})